package com.andriology.ui;

import com.andriology.logic.Test;
import com.andriology.ommar_el_ard.R;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

import com.actionbarsherlock.app.SherlockFragment;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.Tracker;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class AllTestsActivity extends SherlockFragment {

	private String testTitleStr;
	private int testNumber;
	private Test test;
	
	Tracker myTracker;
	
    @Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_all_tests, container, false);
		
		/* Inflate XML string array to Java object array of strings */
        String testTitlesArray []= getResources().getStringArray(R.array.test_names_array);
        
        /* Use adapter array to adapt the string array into List of views*/
        ListView listView = (ListView)view.findViewById(R.id.listViewAllTests);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getSherlockActivity(), 
        															android.R.layout.simple_list_item_1,
        															testTitlesArray);
        listView.setAdapter(arrayAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        	public void onItemClick(AdapterView<?> parent, View v, int position, long id){
        		Intent intent = new Intent(getSherlockActivity(), TestActivity.class);
        		/* position is zero based while the test number is one based.
        		 * Make use of offset between the position in the array adapter and test number
        		 * test number = position +1 */
        		testTitleStr = String.valueOf(((TextView)v).getText());
        		testNumber = position+1;
        		
        		test = new Test(getSherlockActivity(),testNumber,testTitleStr);
        		
        		/* load respective test questions, final messages and advices*/
        		loadTestInformation();
        		
        		/* send test object to single test activity */
        		intent.putExtra("parcel", test);
        		startActivity(intent);
        	}
		});
		
		return view;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		/* Set context */
		EasyTracker.getInstance().setContext(this.getSherlockActivity().getApplicationContext());
		
		/* Instantiate the Tracker */
		myTracker = EasyTracker.getTracker();

		/* Since Action Bar Sherlok inflates action bar typically as Action Bar created by post-HoneyComb versions,
		 * I have to hide navigation up from application icon in the action bar for pre-HoneyComb versions because
		 * it does not respond to application icon button to navigate up*/
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB){
			getSherlockActivity().getSupportActionBar().setDisplayHomeAsUpEnabled(false);
		}


	}
    
	@Override
	public void onStart() {
	  super.onStart();
	  myTracker.sendView("AllTests Screen");
	}
	
    /*@Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	Intent intent;
    	switch (item.getItemId()) {
    	case android.R.id.home:
    		app icon in action bar clicked; go home
    		intent = new Intent(this, MainActivity.class);
            
             With this flag, if the activity you're starting already exists in the current task, 
             * then all activities on top of it are destroyed and it is brought to the front.
             * you should usually not create a new instance of the home activity. Otherwise, 
             * you might end up with a long stack of activities in the current task with multiple 
             * instances of the home activity.
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            return true;

		default:
			return super.onOptionsItemSelected(item);
		}
    }*/
    
	private String[] loadResultMessages(String data){
		/* The data could be either messages or advices since, the defined arrays in xml is either:
		 * messages_test_N or advices_test_N*/
		if (data.equals("messages")|| data.equals("advices")){
			String resourceName = data + "_test_"+testNumber ;
			int resourceId = getResources().getIdentifier(resourceName, "array", getSherlockActivity().getPackageName());
			return getResources().getStringArray(resourceId);
		}
		else{
			return null;
		}
	}

	private int[] loadResultRanges(){
		/* the defined arrays in xml is ranges*/
		int resourceId = getResources().getIdentifier("ranges", "array", getSherlockActivity().getPackageName());
		return getResources().getIntArray(resourceId);
	}
	
	private String[] loadQuestions(){
		String resourceName = "test_"+ testNumber+ "_questions";
		int resrourceId = getResources().getIdentifier(resourceName, "array", getSherlockActivity().getPackageName());
		return getResources().getStringArray(resrourceId);
	}

	private void loadTestInformation(){

        /*testTitleStr = getIntent().getStringExtra("TEST_TITLE");*/
        
        /* load information of result criteria upon which messages and advices will be shown to the user*/
        int []ranges   = loadResultRanges();
        String [] messages = loadResultMessages("messages");
        String [] advices = loadResultMessages("advices");
        test.getResultTestInfo().setRangesMessages(ranges, messages, advices);
        
        /* load all test questions with its corresponding choices in memory */
        String[] questions = loadQuestions();
        test.setQuestions(questions);
	}

}
